package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.business.services.BaseService;
import com.dmcb.common.business.utils.*;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.common.business.utils.WebClientUtil;
import com.dmcb.trade.business.constants.TradeConstant;
import com.dmcb.trade.business.entities.Article;
import com.dmcb.trade.business.mappers.ArticleCrawlMapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;

/**
 * Created by wangyong on 2017/9/15.
 */
@Service
public class YoudiaoCrawler extends BaseService {
    Logger logger = LoggerFactory.getLogger(YoudiaoCrawler.class);
    @Autowired
    private WebClientUtil webClientUtil;

    @Value("${dmr.host}")
    private String dmrHost;

    @Autowired
    private ArticleCrawlMapper articleCrawlMapper;

    public JsonResult crawler() {
        int ero = 0;
        int success = 0;
        int authorId = 5032;
        Document document;
        String json = WebUtil.buildRequest("https://z.diaox2.com/copyright", false);
        JSONArray jsonArray = JsonUtil.readJsonArray(json);
        for (int i = 0; i < jsonArray.size(); i++) {
            WebClientUtil.wc = webClientUtil.initWc(false,false,true);
            JSONObject jsonObject = jsonArray.optJSONObject(i);
            String link = (String) jsonObject.get("url");
            document = webClientUtil.getDoc(link);
            if (document == null) {
                ero++;
                if (ero > 10) {
                    break;
                }
                continue;
            }
            String title = document.select("h1.article-title").text();
            String content = document.select("div.article").toString();
            String body = document.select("div.article").text();
            try {
                body = EncodeUtil.filterOffUtf8Mb4(body);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            String time = jsonObject.get("pubtime")+"";

            String year = time.substring(0, 4);
            String mouth = time.substring(4, 6);
            String day = time.substring(6, 8);
            time = year + "-" + mouth + "-" + day;

            String author = "有调买买君";
            // 校验该作者标题是否存在

            Article article = articleCrawlMapper.queryByAuthorTitle(authorId, title);
            if (article == null) {
                // 新文章
                article = new Article();
                article.setAuthorId(authorId);
                article.setAuthorName(author);
                article.setTitle(title);
                article.setPostTime(DateUtil.parseDate(time));
                article.setLink(link);
                article.setPlatform("有调");
            }
            // 如果已经存在，直接返回
            if (article.getId() > 0) {
                logger.info(TradeConstant.MSG_ERROR_TITLE_EXIST);
                continue;
            }
            article.setBody(body);
            articleCrawlMapper.insert(article);
            String res = WebUtil.sendPost(dmrHost +"/platform/regist/article","mob=18368122887&author="+author+"&title="+title+"&content="+content);
            System.out.println(res);
            success++;
        }
        return success(success);
    }

}
